﻿/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* -------------------------- Start ------------------------------------- */
/*CR ------------------------ Calendar ---------------------------------- */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* 
Example: 
<input class="inputCalendar" type="text" id="" name="" value="" title="format:dd-mon-yyyy;from:12-7-2009;to:objectName" /> 
<input class="inputCalendar" type="text" id="" name="" value="" title="from:12-7-2009;to:12-8-2009" /> 
<input class="inputCalendar" type="text" id="" name="" value="" title="from:objectNameA;to:objectNameB" /> 
<input class="inputCalendar" type="text" id="" name="" value="" /> 
CR.initDateField(calendarInput);
*/ 

var dateErrorMsg = 'Please input a valid date. Date format is ';

var CR = { 
	id : "CR", 
	calendars : new Array(), 
	activeCalendar : null, 
	zIndex : 4100, 
	yearOffset : 100, 
	disableAnimation : true,
	dateFormat : ["dd", "mm", "yyyy", "/"],
	iconMode : false,
	showTextField : false,
	returnWeek : false,
	cR : null,
	init : function(){
		var inputs = CE.getElementsByClassName(document, "input", "inputCalendar");
		var cR = null;
		var clickTarget = null;
		for(var i in inputs){
			inputs[i].k = i;
			this.transformThisObj(inputs[i]);
			clickTarget = (this.iconMode) ? inputs[i].icon : inputs[i];
//			if(inputs[i].disabled != true){
				CE.addEventListener(clickTarget, "click", this.inputOnClick);
				CE.addEventListener(inputs[i], "blur", this.inputOnBlur);
				//CE.addEventListener(inputs[i], "focus", this.inputOnFocus);
//			}
			if(i == 0){
				this.cR = cR = new CALENDAR(inputs[i]);
			}
			this.calendars.push(cR);
			this.drawCalendar(cR.tbe, cR.dateObj); 

			cR.input = inputs[i];	// init title parameter
			if(inputs[i].title != ""){
				CR.handleTitlePmr(cR);
			}			
			inputs[i].transformed = true;
		}		
		CE.addEventListener(document, "click", this.documentOnMouseDown);		
		//CE.addEventListener(document, "dblclick", this.dblclick);		
		//CE.addEventListener(document, "mouseup", this.mouseUp);	
	},
	initDateField : function(tgtObj){
		var inputs = CE.getElementsByClassName(document, "input", "inputCalendar");
		if(tgtObj.transformed == undefined){
			tgtObj.k = inputs.length-1;
			this.transformThisObj(tgtObj);
			clickTarget = (this.iconMode) ? tgtObj.icon : tgtObj;
//			if(tgtObj.disabled != true){
				CE.addEventListener(clickTarget, "click", this.inputOnClick);
				CE.addEventListener(tgtObj, "blur", this.inputOnBlur);
				//CE.addEventListener(tgtObj, "focus", this.inputOnFocus);
//			}
			this.calendars.push(this.cR);
			this.drawCalendar(this.cR.tbe, this.cR.dateObj); 
			this.cR.input = tgtObj;	// init title parameter 
			if(tgtObj.title != ""){
				CR.handleTitlePmr(this.cR);
			}
			tgtObj.transformed = true;
		}
	 
	},
	transformThisObj : function(input){
		if(!CR.showTextField){
			input.readOnly = true;
		}
		//input.style.paddingRight = "10px";
		input.maxLength = 10;

//		if(input.returnWeek){
			var _weekText = document.createElement("b");
			_weekText.style.fontWeight = "normal";
			_weekText.innerHTML = "&nbsp;";
			_weekText.className = "weekText";
			CE.insertAfter(input, _weekText);
			input.weekText = _weekText;
			_weekText.parentInput = input;
//		}		

		if(this.iconMode){
			var _icon = document.createElement("b");
			_icon.innerHTML = "&nbsp;";
			_icon.className = "calenderIcon";
			//CE.$append(input, _icon);
			CE.insertAfter(input, _icon);
			input.icon = _icon;
			input.className = input.className + " iconMode";
			_icon.parentInput = input;
		}
		//input.disabled = true;
	},
	focusTo : function (obj){
		function focusAct(){
			obj.focus();
			obj.select();
		}								
		setTimeout(focusAct, 0);
	},
	handleTitlePmr : function (cR){
		var input = cR.input;		
		var pAry = CE.pmrsToArray(input.title);
		for(var i=0; i<pAry.length; i++){
			if(pAry[i][0] == "format"){
				cR.dateFormat = DE.dateFormatStrToAry(pAry[i][1]);
				input.dateFormat = cR.dateFormat;
			}else if(pAry[i][0] == "from"){
				//pAry[i][1] = CR.getDateStr(pAry[i][1]);
				//this.from = DE.strToDateObj(pAry[i][1]); 
				cR.from = pAry[i][1]; 
				input.from = cR.from;
			}else if(pAry[i][0] == "to"){
				//pAry[i][1] = CR.getDateStr(pAry[i][1]);
				cR.to = pAry[i][1]; 
				input.to = cR.to;
			}else if(pAry[i][0] == "returnWeek"){
				cR.returnWeek = pAry[i][1]; 
				input.returnWeek = cR.returnWeek;
			}
		}
		input.title = "";
	},	
	inputOnFocus : function(e){
		var thisObj = CE.eventObj(e);
		var k = thisObj.k;
		var cR = CR.calendars[k];
		cR.input = thisObj;
		if(thisObj.title != ""){
			CR.handleTitlePmr(cR);
		}		
	},
	inputOnBlur : function(e){
		var thisObj = CE.eventObj(e);
		var k = thisObj.k;
		var value = thisObj.value;
		var cR = CR.calendars[k];		
		//multiCalendar
		cR.input = thisObj;

		var _dateObj = new Date();

		var bdr = cR.bdr;
		var tbe = cR.tbe;
		bdr.style.zIndex = CR.zIndex + 1;
		CR.zIndex++;
		
		CR.activeCalendar = cR;
		CR.hideCalendars(false);		
		CE.pty(thisObj);
		CE.pty(bdr);
		CE.stopPropagation(e);
		
		if(CE.trim(value) != ""){
/*			
			var detectedDateFormat = DE.getDateFormat(value);
			if(detectedDateFormat == null){
				cR.dateFormat = CR.dateFormat;
				_dateObj = null;
			}else{
				cR.dateFormat = detectedDateFormat;
				_dateObj = DE.strToDateObj(value, cR.dateFormat);
			} 
*/
			_dateObj = DE.strToDateObj(value, CR.dateFormat); 
			if(_dateObj == null){
				var dateFormatStr = CR.dateFormat[0] + CR.dateFormat[3] + CR.dateFormat[1] + CR.dateFormat[3] + CR.dateFormat[2];
				alert(dateErrorMsg + " \[ " + dateFormatStr + " \]" );
				//cR.input.value = "";
				//cR.input.focus();
				CR.focusTo(cR.input);
			}else{				
				cR.input.value = DE.dateObjToStr(_dateObj, CR.dateFormat);
			}
			CR.returnWeekText(cR.input, _dateObj);			
		}else{
			CR.returnWeekText(cR.input, null);			
		}		
	},
	returnWeekText : function(input, dateObj){
		if(input.returnWeek){
			if(input.weekText){
				if(dateObj){
					input.weekText.innerHTML = "&nbsp;(" + DE.weekText[1][dateObj.getDay()] + ")&nbsp;";
				}else{
					input.weekText.innerHTML = "&nbsp;";	
				}
			}
		}
	},
	inputOnClick : function(e){
		var doc = CE.docPty();
		var thisObj = CE.eventObj(e);
		if(CR.iconMode){
			var icon = CE.eventObj(e);
			thisObj = icon.parentInput;
		}
		var k = thisObj.k;
		var value = thisObj.value;
		var cR = CR.calendars[k];
		//multiCalendar
		cR.input = thisObj;
		/* ---------------------------------------------------------------------------- */		
		if(thisObj.title != ""){
			CR.handleTitlePmr(cR);
		}else{
			if(thisObj.from){
				cR.from = thisObj.from; 
			}else{
				cR.from = ""; 
			}
			if(thisObj.to){
				cR.to = thisObj.to; 
			}else{
				cR.to = ""; 
			}
		}

		/* ---------------------------------------------------------------------------- */		

		var _dateObj = new Date();
		var bdr = cR.bdr;
		var tbe = cR.tbe;
		bdr.style.zIndex = CR.zIndex + 1;
		CR.zIndex++;
		
		CR.activeCalendar = cR;
		CR.hideCalendars(false);		
		CE.pty(thisObj);
		CE.pty(bdr);
		CE.stopPropagation(e);
		
		if(cR.dateFormat == undefined){
			var detectedDateFormat = DE.getDateFormat(value);
			if(detectedDateFormat == null){
				//default date format
				cR.dateFormat = CR.dateFormat;
			}else{
				cR.dateFormat = detectedDateFormat;
			}
		}

		_dateObj = DE.strToDateObj(value, cR.dateFormat); 
		
		if(_dateObj == null){
			_dateObj = new Date();
		}
		
		if(_dateObj != null){
			//tbe.focus();
			cR.dateObj = _dateObj;
			var selectedDate = new Date();
			selectedDate.setFullYear(_dateObj.getFullYear());				
			selectedDate.setMonth(_dateObj.getMonth(), _dateObj.getDate());
			cR.selectedDate = selectedDate;		

			CR.drawCalendar(tbe, _dateObj);
			//CE.setAlpha(bdr, 0);
			var newY = 0;
			var newX = 0;
				
			if(thisObj.Y + thisObj.H + bdr.H >= doc.H){
				if(CR.showTextField == true){
					newY = thisObj.Y - bdr.H;
				}else{
					newY = thisObj.Y - bdr.H + thisObj.H - 1;
				}
			}else{
				if(CR.showTextField == true){
					newY = thisObj.Y + thisObj.H;
				}else{
					newY = thisObj.Y - 1;
				}
			}
			if(thisObj.X + thisObj.W + bdr.W >= doc.W){
				newX = thisObj.X - (bdr.W - thisObj.W) - 1;
			}else{
				//newX = thisObj.X;
				newX = thisObj.X - 1;
			}
		}
//		else{
//			thisObj.value = "";
//		}
		function _transformCompleted(e){
			if(_dateObj != null){
				function _tweenCompleted(){
					bdr.style.top = newY + "px";
					bdr.style.left = newX + "px";					
					//var _tweenBdrA = Tween(bdr, "alpha", Popup.easeOut, 0, 1);
				}
				//thisObj.value = "";
				//var _tween = Tween(thisObj, "width", Strong.easeOut, thisObj.W, 19, 10, false); 
				//var _tweenA = Tween(thisObj, "alpha", Strong.easeOut, 1, 0, 15, false);			
				//_tween.addEventListener("MOTION_FINISH", _tweenCompleted);
				_tweenCompleted();
			}
		}
		var _f = new p(thisObj.X+(thisObj.W-22), thisObj.Y, null, thisObj.H, thisObj.H, null);
		var _t = new p(newX-10, newY-10, null, bdr.W, bdr.H, null);
		_transformCompleted();
		//AN.box(_f, _t, Popup.easeOut, _transformCompleted, 13, false);
/*
		CE.alert("clientWidth_" + thisObj.clientWidth + "_" + 
				 "offsetWidth_" + thisObj.offsetWidth + "_" + 
				 "clientLeft_" + thisObj.clientLeft + "_" + 
				 "offsetLeft_" + thisObj.offsetLeft + "_" + 
				 "padding_" + thisObj.padding + "_" + 				 
				 ""				 
				 );
		thisObj.style.width = thisObj.clientWidth + "px";
		return;
*/
	},	
	returnValueToField : function(k, _dateObj){	
		var cR = CR.calendars[k];
		var input = cR.input;
		var bdr = cR.bdr;
		function _backTweenCompleted(){
			if(_dateObj == null){
				input.value = ""; 
			}else{
				input.value = DE.dateObjToStr(_dateObj, CR.dateFormat);
			}
			CR.returnWeekText(input, _dateObj);							
		}
		function _tweenCompleted(){
			//AN.backward(input, Strong.easeOut, 10, _backTweenCompleted);
			bdr.style.left = "-1000px";
			bdr.style.top = "-1000px";
			_backTweenCompleted();
		}
		//var _tweenA = Tween(bdr, "alpha", Strong.easeOut, 1, 0, 15, false);			
		//_tweenA.addEventListener("MOTION_FINISH", _tweenCompleted);			
		_tweenCompleted();
	},
	getDateStr : function(str){
		if(str == undefined)
			return null;
		var _str = str;
		if(CE.$(_str) != null){
			var _obj = CE.$(_str);
			if(_obj.tagName.toLowerCase() == "input"){
				_str = _obj.value;
			}else{
				if(DE.strToDateObj(_obj.innerHTML) != null)
					_str = _obj.innerHTML;
			}
		}
		return _str;
	},
	mouseDown : function(e){
	},	
	mouseUp : function(e){ 

	},
	documentOnMouseDown : function(e){
		CR.hideCalendars(true);
	},
	hideCalendars : function(_delAll){	
		var cRs = CR.calendars;
		for(var i=0; i<cRs.length; i++){
			if(_delAll){
				CR.hideCalendar(cRs[i]);
			}else{
				if(cRs[i] != CR.activeCalendar)				
					CR.hideCalendar(cRs[i]);
			}
		}
	},
	hideCalendar : function(cr){
		function _backTweenCompleted(){
		}
		var preW = CE.pty(cr.input).preW;
		if(preW != null)
			CE.setWidth(cr.input, preW);
		//AN.backward(cr.input, Strong.easeOut, 10, _backTweenCompleted);		
		cr.bdr.style.left = "-1000px";
		cr.bdr.style.top = "-1000px";
	},
	drawCalendar : function(tbe, dateObj){
		var k = tbe.k;
		var cR = CR.calendars[k];
		var selectedDate = cR.selectedDate;
		
		CR.rebuildYear(cR.yearSelect, dateObj);
		
		var from = CR.getDateStr(cR.from);
		from = DE.strToDateObj(from);
		var to = CR.getDateStr(cR.to);
		to = DE.strToDateObj(to); 
		
		var _dateObj = new Date();
		_dateObj.setFullYear(dateObj.getFullYear());
		_dateObj.setMonth(dateObj.getMonth());		
		_dateObj.setDate(dateObj.getDate());
		_dateObj.setHours(0,0,0,0);

		tbe.border = 0;
		function emptyCell(cell){
			cell.innerHTML = "&nbsp;";
			cell.className = "cell cellOutOfMonth";
		}
		
		var today = new Date();
		var year = _dateObj.getFullYear();
		var month = _dateObj.getMonth();	// 0-11
		
		var day = null; // sun, mon, tue
		var date = null; // 1-31 
		var c = 0; //dateCount
		c++;
		_dateObj.setDate(c);
		day = _dateObj.getDay(); // sun, mon, tue
		date = _dateObj.getDate(); // 1-31 
		var started = false;
		for(var i=0; i<tbe.rows.length; i++){
			var row = tbe.rows[i];
			for(var j=0; j<row.cells.length; j++){
				var cell = row.cells[j];
				cell.k = tbe.k;
				if(i == 0){
					cell.innerHTML = DE.weekText[2][j];
					cell.title = DE.weekText[0][j];					
				}else{
					if(day == j){
						started = true;
					}else if(!started){
						emptyCell(cell);
					}
					if(started){
						var tmpMonth = _dateObj.getMonth();
						if(month == tmpMonth){
							CE.removeEventListener(cell, "click", cR.cellOnClick);
							cell.innerHTML = date;
							cell.date = date;
							if(	today.getDate() == _dateObj.getDate() && 
								today.getMonth() == _dateObj.getMonth() &&
								today.getFullYear() == _dateObj.getFullYear()
								)
							{	
								cell.className = "cell cellToday";
								cell.title = "Today";
								if(DE.withInValidRange(_dateObj, from, to)){								
									CE.addEventListener(cell, "click", cR.cellOnClick);
									cell.style.cursor = "pointer";
								}
							}else if(selectedDate.getDate() == _dateObj.getDate() && 
								selectedDate.getMonth() == _dateObj.getMonth() &&
								selectedDate.getFullYear() == _dateObj.getFullYear()
								)
							{
								cell.className = "cell cellSelected";
								cell.title = "Selected Date";
								CE.addEventListener(cell, "click", cR.cellOnClick);
								cell.style.cursor = "pointer";
							}else{								
								if(!DE.withInValidRange(_dateObj, from, to)){
									cell.className = "cell cellOutRangeInMonth";
								}else{
									cell.className = "cell cellWithInMonth";
									CE.addEventListener(cell, "click", cR.cellOnClick);
									cell.style.cursor = "pointer";
								}
							}							
							c++;
							_dateObj.setDate(c);
							date = _dateObj.getDate(); // 1-31
						}else{
							emptyCell(cell);
						}
					}
				}
			}
		}
		CR.synMonthYearSelect(k); 
	},
	drawButton : function(btns, btnName, title){
		function btnOnMouseOver(e){
			thisObj = CE.eventObj(e);
			thisObj.className = thisObj.className + "Over";
		}
		function btnOnMouseOut(e){
			thisObj = CE.eventObj(e);
			thisObj.className = thisObj.className.replace("Over", "");
		}
		function btnOnClick(e){
			var thisObj = CE.eventObj(e);
			var k = thisObj.k;
			var cR = CR.calendars[k];
			var bdr = cR.bdr;
			var _dateObj = cR.dateObj;
			if(thisObj.id == "mLeft" || thisObj.id == "mRight"){				
				var newMonth = null;
				var oldMonth = _dateObj.getMonth();		
				if(thisObj.id == "mLeft"){
					newMonth = oldMonth - 1;
				}else if(thisObj.id == "mRight"){
					newMonth = oldMonth + 1;
				}
				_dateObj.setDate(1);
				_dateObj.setMonth(newMonth);
			}else if(thisObj.id == "yLeft" || thisObj.id == "yRight"){				
				var newYear = null;
				var oldYear = _dateObj.getFullYear();
				if(thisObj.id == "yLeft"){
					newYear = oldYear - 1;
				}else if(thisObj.id == "yRight"){
					newYear = oldYear + 1;
				}
				_dateObj.setFullYear(newYear);
			}else if(thisObj.id == "today"){
				var newDate = new Date();	
				var from = CR.getDateStr(cR.from);
				from = DE.strToDateObj(from);
				var to = CR.getDateStr(cR.to);
				to = DE.strToDateObj(to); 				
				if(DE.withInValidRange(newDate, from, to)){ 
					CR.returnValueToField(k, newDate);
				}
			}else if(thisObj.id == "erase"){
				var newDate = null;	
				CR.returnValueToField(k, newDate);
			}				
			var tbe = CE.getElementsByClassName(bdr, "table", "tbe", true)[0];
			cR.dateObj.dateObj = _dateObj;						
			CR.drawCalendar(tbe, _dateObj);
		}
		btns.rows[0].insertCell(-1);
		var tmpObj = CE.newObj("div", btnName, btnName, "btn " + btnName, btns.k);
		tmpObj.title = title;
		CE.addEventListener(tmpObj, "mouseover", btnOnMouseOver);
		CE.addEventListener(tmpObj, "mouseout", btnOnMouseOut);
		CE.addEventListener(tmpObj, "click", btnOnClick);
		btns.rows[0].cells[btns.rows[0].cells.length-1].appendChild(tmpObj);		
		return tmpObj;		
	},
	synMonthYearSelect : function(k){
		var cR = CR.calendars[k];
		var monthSelect = cR.monthSelect;
		var yearSelect = cR.yearSelect;
		var _dateObj = cR.dateObj;
		monthSelect.selectedIndex = _dateObj.getMonth();  
		CE.select(yearSelect, "text", _dateObj.getFullYear());
	},
	drawMonth : function(btns, cName){
		function monthSelectOnChange(e){
			thisObj = CE.eventObj(e);
			var k = thisObj.k;
			var cR = CR.calendars[k];
			var _dateObj = cR.dateObj;			
			_dateObj.setMonth(thisObj.selectedIndex, 1);
			var tbe = cR.tbe;
			cR.dateObj = _dateObj;						
			CR.drawCalendar(tbe, _dateObj);
		}
		var now = new Date();		
		btns.rows[0].insertCell(-1);
		var tmpObj = CE.newObj("select", cName+btns.k, cName+btns.k, cName, btns.k);
		for(var i=0; i<DE.monthText[1].length; i++){
			var tmpOpt = CE.newObj("option", "", "", "", btns.k);
			tmpOpt.innerHTML = DE.monthText[1][i];
			tmpOpt.value = i;
			if(now.getMonth() == i){
				tmpOpt.selected = true;
			}
			tmpObj.appendChild(tmpOpt);
		}
		CE.addEventListener(tmpObj, "change", monthSelectOnChange);
		btns.rows[0].cells[btns.rows[0].cells.length-1].appendChild(tmpObj);
		return tmpObj;		
	},
	rebuildYear : function(yearSelect, dateObj){
		var _dateObj = dateObj;
		var _year = _dateObj.getFullYear();
		var _start = eval(_year - CR.yearOffset);
		for(var i=0; i<yearSelect.options.length; i++){
			var opt = yearSelect.options[i];
			opt.text = _start + i;
			opt.value = _start + i;
			if(opt.value == _year){
				yearSelect.selectedIndex = i;
			}
		}
	},
	drawYear : function(btns, cName){
		function yearSelectOnChange(e){
			thisObj = CE.eventObj(e);
			var k = thisObj.k;
			var cR = CR.calendars[k];
			var _dateObj = cR.dateObj;
			_dateObj.setFullYear(CE.selected(thisObj, "value"));			
			var tbe = cR.tbe;
			cR.dateObj = _dateObj;						
			CR.drawCalendar(tbe, _dateObj);
		}		
		var offset = CR.yearOffset;
		var now = new Date();
		btns.rows[0].insertCell(-1);
		var tmpObj = CE.newObj("select", cName+btns.k, cName+btns.k, cName, btns.k);
		for(var i=0; i<offset*2; i++){
			var tmpOpt = CE.newObj("option", "", "", "", btns.k);
			tmpOpt.innerHTML = i;
			tmpOpt.value = i;
			tmpObj.appendChild(tmpOpt);
		}	
		CR.rebuildYear(tmpObj, now);				
		CE.addEventListener(tmpObj, "change", yearSelectOnChange);				
		btns.rows[0].cells[btns.rows[0].cells.length-1].appendChild(tmpObj);
		return tmpObj;
	}
}
function CALENDAR(input){
	this.cellOnMouseOver = function(e){
		var thisObj = CE.eventObj(e);
		if(thisObj.className.indexOf("cellWithInMonth") != -1)
			thisObj.className = thisObj.className + " cellMouseOver";
	}
	this.cellOnMouseOut = function(e){
		var thisObj = CE.eventObj(e);
		thisObj.className = thisObj.className.replace(" cellMouseOver", "");
	}
	this.cellOnClick = function(e){
		var thisObj = CE.eventObj(e);
		var k = thisObj.k;
		var cR = CR.calendars[k];
		var _dateObj = cR.dateObj;
		var bdr = cR.bdr;
		if(thisObj.className.indexOf("cellOutOfMonth") == -1 &&
			thisObj.className.indexOf("cellOutRangeInMonth") == -1 ){
			_dateObj.setDate(thisObj.date);			
			CR.returnValueToField(k, _dateObj);
		}
	}

	this.k = input.k;	
	this.id = "calendar_object_" + this.k;	
	var k = this.k;

	var bdr = CE.newObj("div", "bdr"+k, "bdr"+k, "CR", k);	
	bdr.style.zIndex = CR.zIndex + 1;
	CR.zIndex++;
	this.input = input; 
	var btns = CE.newObj("table", "btns"+k, "btns"+k, "btns", k);
	btns.border = 0;
	btns.cellPadding = 0;
	btns.cellSpacing = 0;	
	btns.insertRow(-1);
	
//	this.yLeft = CR.drawButton(btns, "yLeft", "Previous Year");	
//	this.yRight = CR.drawButton(btns, "yRight", "Next Year");	
	this.mLeft = CR.drawButton(btns, "mLeft", "Previous Month");	
	this.today = CR.drawButton(btns, "today", "Today");	
	this.erase = CR.drawButton(btns, "erase", "Delete");	
	this.monthSelect = CR.drawMonth(btns, "monthSelect");	
	this.yearSelect = CR.drawYear(btns, "yearSelect");		
	this.mRight = CR.drawButton(btns, "mRight", "Next Month");	
	
	var tbe = CE.newObj("table", "bdr"+k, "bdr"+k, "tbe", k);
	tbe.border = 0;
	tbe.cellPadding = 0; 
	tbe.cellSpacing = 0; 
	for(var i=0; i<7; i++){
		tbe.insertRow(-1);
		tbe.rows[i].className = "row";
		for(var j=0; j<7; j++){
			tbe.rows[i].insertCell(-1);
			var cell = tbe.rows[i].cells[j];
			cell.className = "cell";
			if(i==0){ // week header.
				cell.className = "cell cellWeekHdr";
			}else{
				cell.className = "cell cellOutOfMonth";	
				CE.addEventListener(cell, "mouseover", this.cellOnMouseOver);
				CE.addEventListener(cell, "mouseout", this.cellOnMouseOut);
				//CE.addEventListener(cell, "click", this.cellOnClick);
			}
			tbe.rows[i].cells[j].innerHTML = i + "." + j;
		}
	}
	
	if(CE.browser("IE6")){
		//tmp div to group the btns and tbe for ie6
		var tmpBdr = CE.newObj("div", "tmpBdr"+k, "tmpBdr"+k, "tmpCR", k);	
		tmpBdr.style.border = "0px solid green";	
		tmpBdr.style.display = "block";	
		tmpBdr.appendChild(btns);
		tmpBdr.appendChild(tbe);
		CE.iframeMask(bdr, tmpBdr);
		this.tmpBdr = tmpBdr;	
		bdr.appendChild(tmpBdr);
	}else{
		bdr.appendChild(btns);
		bdr.appendChild(tbe);
	}
	
	this.bdrOnClick = function(e){
		//var thisObj = CE.eventObj(e);
		//var k = thisObj.k;
		//var cR = CR.calendars[k];
		//CE.alert("bdrOnClick");
		CE.stopPropagation(e);
	}
	CE.addEventListener(bdr, "click", this.bdrOnClick);

	var dateObj = new Date();
	dateObj.setDate(1);
	this.dateObj = dateObj;
	this.selectedDate = dateObj;	
	this.bdr = bdr;
	this.tbe = tbe;	
	document.body.appendChild(bdr);		
	if(CE.browser("IE6")){	
		CE.pty(tmpBdr);
		CE.setHeight(bdr, tmpBdr.H);
	}
	return this;
}

//CR.init();

/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* -------------------------- End --------------------------------------- */
/* -------------------------- Calendar ----------------------------------- */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */









